package com.lishu.carsafe.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lishu.carsafe.R;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/5/21
 * time:14:54
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class AppDialogUtils {


    private static AlertDialog mAlertDialog;

    /**
     * 请假弹窗
     * @param activity
     * @param view
     * @param clickListener
     */
    public static void showLeaveDialog(final Activity activity, View view, final View.OnClickListener clickListener){
        if(activity != null && !activity.isFinishing()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setCancelable(true);
            TextView tv_dialog_confirm = view.findViewById(R.id.tv_dialog_confirm);
            TextView tv_dialog_cancel = view.findViewById(R.id.tv_dialog_cancel);
            final EditText et_dialog_content = view.findViewById(R.id.et_dialog_content);

            tv_dialog_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            tv_dialog_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String content = et_dialog_content.getText().toString().trim();
                    if(TextUtils.isEmpty(content)){
                        ToastUtil.show(activity,"请输入请假理由", Toast.LENGTH_LONG);
                        return;
                    }
                    if(clickListener != null){
                        dismiss();
                        v.setTag(content);
                        clickListener.onClick(v);
                    }
                }
            });

            builder.setView(view);
            mAlertDialog = builder.create();
            mAlertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            mAlertDialog.getWindow().setGravity(Gravity.CENTER);
            mAlertDialog.setCancelable(false);
            mAlertDialog.setCanceledOnTouchOutside(false);
            mAlertDialog.show();
            clearDialogBackground();
        }
    }

    public static void dismiss(){
        if(mAlertDialog != null){
            mAlertDialog.dismiss();
            mAlertDialog = null;
        }
    }


    public static void clearDialogBackground() {
        clearDialogBackground(mAlertDialog);
    }

    public static void clearDialogBackground(AlertDialog mAlertDialog) {
        OSUtils.ROM romType = OSUtils.getRomType();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || romType == OSUtils.ROM.YuLong) {
            ViewGroup vg = mAlertDialog.findViewById(android.view.Window.ID_ANDROID_CONTENT);

            for (int i = 0, j = vg.getChildCount(); i < j; i++) {
                View v2 = vg.getChildAt(i);
                if (v2 instanceof ViewGroup) {
                    ViewGroup vg2 = (ViewGroup) v2;
                    for (int k = 0, l = vg2.getChildCount(); k < l; k++) {
                        vg2.getChildAt(k).setBackgroundColor(0x00000000);
                    }
                }
            }
        }
    }
}
